package com.mt.customer.service.model;

import java.util.List;
import java.util.Map;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import com.github.pagehelper.PageHelper;
import com.mt.common.PagerInfo;
import com.mt.common.ServiceResult;
import com.mt.customer.entity.CustomerAccountEntity;
import com.mt.customer.entity.CustomerAccountLogEntity;
import com.mt.customer.mapper.CustomerAccountEntityMapper;
import com.mt.customer.mapper.CustomerAccountLogEntityMapper;

@Component
public class CustomerAccountServiceModel {
    private static final org.slf4j.Logger LOGGER = LoggerFactory.getLogger(CustomerAccountServiceModel.class);

    @Autowired
    private CustomerAccountEntityMapper customerAccountEntityMapper;

    @Autowired
    private CustomerAccountLogEntityMapper customerAccountLogEntityMapper;

    public ServiceResult<List<CustomerAccountEntity>> selectList(Map<String, Object> params, PagerInfo<?> pagerInfo) {
        Assert.notNull(this.customerAccountEntityMapper, "Property 'customerAccountEntityMapper' is required.");
        ServiceResult<List<CustomerAccountEntity>> result = new ServiceResult<List<CustomerAccountEntity>>();
        try {
            if (pagerInfo != null) {
                PageHelper.startPage(pagerInfo.getPageIndex(), pagerInfo.getPageSize());
            }

            result.setResult(this.customerAccountEntityMapper.selectList(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerAccountServiceModel][selectList]：query selectList occur exception", e);
        }
        return result;
    }

    public ServiceResult<CustomerAccountEntity> selectInfo(Map<String, Object> params) {
        Assert.notNull(this.customerAccountEntityMapper, "Property 'customerAccountEntityMapper' is required.");
        ServiceResult<CustomerAccountEntity> result = new ServiceResult<CustomerAccountEntity>();
        try {
            result.setResult(this.customerAccountEntityMapper.selectByPrimaryKey(params));
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerAccountServiceModel][selectInfo]：query selectInfo occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> insert(CustomerAccountEntity customerAccountEntity) {
        Assert.notNull(this.customerAccountEntityMapper, "Property 'customerAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.customerAccountEntityMapper.insert(customerAccountEntity);
            if (save > 0) {
                id = customerAccountEntity.getCustomerAccountId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerAccountServiceModel][insert]：query insert occur exception", e);
        }
        return result;
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000,
        rollbackFor = Exception.class)
    public ServiceResult<Integer> updateRelate(CustomerAccountEntity customerAccountEntity) {
        Assert.notNull(this.customerAccountEntityMapper, "Property 'customerAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.customerAccountEntityMapper.updateByPrimaryKeySelective(customerAccountEntity);
            if (save > 0) {
                id = customerAccountEntity.getCustomerAccountId();
                // 编辑操作，先删除原有用户角色关联数据，再创建
                List<CustomerAccountLogEntity> customerAccountLogList =
                    customerAccountEntity.getCustomerAccountLogList();
                if (customerAccountLogList != null && customerAccountLogList.size() > 0) {
                    customerAccountLogEntityMapper.insertList(customerAccountLogList);
                }
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerAccountServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
            throw new RuntimeException(e.getMessage());
        }
        return result;
    }

    public ServiceResult<Integer> updateByPrimaryKeySelective(CustomerAccountEntity customerAccountEntity) {
        Assert.notNull(this.customerAccountEntityMapper, "Property 'customerAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int id = 0;
        try {
            int save = this.customerAccountEntityMapper.updateByPrimaryKeySelective(customerAccountEntity);
            if (save > 0) {
                id = customerAccountEntity.getCustomerAccountId();
            }
            result.setResult(id);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerAccountServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }

    public ServiceResult<Integer> deleteByPrimaryKey(Integer customerAccountId) {
        Assert.notNull(this.customerAccountEntityMapper, "Property 'customerAccountEntityMapper' is required.");
        ServiceResult<Integer> result = new ServiceResult<Integer>();
        int save = 0;
        try {
            save = this.customerAccountEntityMapper.deleteByPrimaryKey(customerAccountId);
            result.setResult(save);
        } catch (Exception e) {
            result.setSuccess(false);
            result.setMessage("Unknown error!");
            LOGGER.error("[CustomerAccountServiceModel][updateByPrimaryKeySelective]:query update occur exception", e);
        }
        return result;
    }
}
